package com.cloudeasy.area.controller;

import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.area.AreaVO;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.area.dto.AreaDTO;
import com.cloudeasy.area.po.Area;
import com.cloudeasy.area.service.AreaAttentionService;
import com.cloudeasy.area.service.AreaService;
import com.cloudeasy.area.util.AreaUtil;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class AreaController {
    private final AreaService areaService;

    public AreaController(AreaService areaService) {
        this.areaService = areaService;
    }

    /**
     * 添加区域
     *
     * @param areaDTO {@link AreaDTO}
     * @return {@link Result<AreaVO>}
     */
    @PostMapping
    public Result<AreaVO> save(@RequestBody @Validated AreaDTO areaDTO) {
        return ResultUtil.success(AreaUtil.convert(areaService.save(areaDTO)));
    }

    /**
     * 更新区域
     *
     * @param id      Id
     * @param areaDTO {@link AreaDTO}
     * @return {@link Result<AreaVO>}
     */
    @PutMapping("/{id}")
    public Result<AreaVO> update(@PathVariable("id") String id, @RequestBody @Validated AreaDTO areaDTO) {
        return ResultUtil.success(AreaUtil.convert(areaService.update(id, areaDTO)));
    }

    /**
     * 删除区域
     *
     * @param id Id
     * @return {@link Result}
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        areaService.delete(id);
        return ResultUtil.success();
    }

    /**
     * 查询区域信息
     *
     * @return {@link List<AreaVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<AreaVO>> list() {
        return ResultUtil.success(AreaUtil.convert(areaService.list()));
    }

    /**
     * 查询区域信息
     *
     * @param ids Id列表
     * @return {@link List<AreaVO>}的{@link Result}
     */
    @GetMapping("/ids")
    public Result<List<AreaVO>> listByIds(@RequestParam("ids") List<String> ids) {
        return ResultUtil.success(AreaUtil.convert(areaService.listByIds(ids)));
    }

    /**
     * 查询区域信息
     *
     * @param name 名称
     * @return {@link List<AreaVO>}的{@link Result}
     */
    @GetMapping("/name")
    public Result<List<AreaVO>> listByName(@RequestParam("name") String name) {
        return ResultUtil.success(AreaUtil.convert(areaService.listByName(name)));
    }

    /**
     * 分页查询区域信息
     *
     * @param page 页数
     * @param size 每页大小
     * @return {@link PageResult<AreaVO>}的{@link Result}
     */
    @GetMapping("/{page}/{size}")
    public Result<PageResult<AreaVO>> page(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return ResultUtil.success(AreaUtil.convert(areaService.page(page, size)));
    }

    /**
     * 分页查询区域信息
     *
     * @param ids  Id列表
     * @param page 页数
     * @param size 每页大小
     * @return {@link PageResult<AreaVO>}的{@link Result}
     */
    @GetMapping("/ids/{page}/{size}")
    public Result<PageResult<AreaVO>> pageByIds(@RequestParam("ids") List<String> ids, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return ResultUtil.success(AreaUtil.convert(areaService.pageByIds(ids, page, size)));
    }

    /**
     * 分页查询区域信息
     *
     * @param name 名称
     * @param page 页数
     * @param size 每页大小
     * @return {@link Page<Area>}的{@link Result}
     */
    @GetMapping("/name/{page}/{size}")
    public Result<PageResult<AreaVO>> pageByName(@RequestParam("name") String name, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return ResultUtil.success(AreaUtil.convert(areaService.pageByName(name, page, size)));
    }
}
